package leetcode.L24;

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode(0, head);
        ListNode prev = dummy;
        ListNode first = head;
        if (head==null) {
            return null;
        }
        ListNode second = head.next;
        while (first != null && second != null) {
            //swap
            prev.next=second;
            first.next=second.next;
            second.next=first;
            prev=first;
            first=first.next;
            if (first != null&&first.next != null) {
                second = first.next;
            }
        }
        return dummy.next;
    }
}